<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
<script>
// 接收一个promise数组，返回一个promise，返回的promise的值为数组中第一个状态改变的promise的值
let promiseRace = (array) => {
    if (!Array.isArray(array)) {
        throw new Error('要返回数组')
    }

    return new Promise((resolve, reject) => {
        array.forEach((item) => {
            if (item instanceof Promise) {
                item.then(res => {
                    return resolve(res)
                },
                    err => reject(err))
            } else {
                return resolve(item)
            }
        })
    })
}
const p1 = new Promise((resolve) => {
    setTimeout(() => {
        resolve("p1");
    }, 3000)
})
const p2 = new Promise((resolve) => {
    setTimeout(() => {
        resolve("p2");
    }, 2000)
})
const p3 = new Promise((resolve) => {
    setTimeout(() => {
        resolve("p3");
    }, 1000)
})

promiseRace([p1, p2, p3]).then(console.log); // p3
</script>
</body>
</html>